from django.contrib import admin
from .models import FlowerCategory, Flower, FlowerImage, FlowerSpec


@admin.register(FlowerCategory)
class FlowerCategoryAdmin(admin.ModelAdmin):
    list_display = ['name', 'sort_order', 'is_active', 'created_at']
    list_filter = ['is_active', 'created_at']
    search_fields = ['name', 'description']
    ordering = ['sort_order', '-created_at']
    list_editable = ['sort_order', 'is_active']


class FlowerImageInline(admin.TabularInline):
    model = FlowerImage
    extra = 1
    fields = ['image_url', 'is_main', 'sort_order']


class FlowerSpecInline(admin.TabularInline):
    model = FlowerSpec
    extra = 1
    fields = ['name', 'price', 'stock', 'is_active', 'sort_order']


@admin.register(Flower)
class FlowerAdmin(admin.ModelAdmin):
    list_display = ['name', 'category', 'price', 'stock', 'sales_count', 'status', 'is_featured', 'sort_order', 'created_at']
    list_filter = ['category', 'status', 'is_featured', 'created_at']
    search_fields = ['name', 'description']
    ordering = ['sort_order', '-created_at']
    list_editable = ['price', 'stock', 'status', 'is_featured', 'sort_order']
    inlines = [FlowerImageInline, FlowerSpecInline]
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'category', 'description')
        }),
        ('价格库存', {
            'fields': ('price', 'original_price', 'stock')
        }),
        ('状态设置', {
            'fields': ('status', 'is_featured', 'sort_order')
        }),
        ('统计信息', {
            'fields': ('sales_count',),
            'classes': ('collapse',)
        })
    )
    
    readonly_fields = ['sales_count']


@admin.register(FlowerImage)
class FlowerImageAdmin(admin.ModelAdmin):
    list_display = ['flower', 'is_main', 'sort_order', 'created_at']
    list_filter = ['is_main', 'created_at']
    search_fields = ['flower__name']
    ordering = ['flower', 'sort_order']


@admin.register(FlowerSpec)
class FlowerSpecAdmin(admin.ModelAdmin):
    list_display = ['flower', 'name', 'price', 'stock', 'is_active', 'sort_order']
    list_filter = ['is_active', 'created_at']
    search_fields = ['flower__name', 'name']
    ordering = ['flower', 'sort_order']
    list_editable = ['price', 'stock', 'is_active', 'sort_order']